var columnid=0;
var last=-1;
var matrix=[];
var table;
var $d=document;
var dir='desc'
var getEl=function(str){
    return $d.getElementById(str);
}

function sortTable(colid,sorttype){
    columnid=colid;
    adjustArrows();
    if(	last==columnid ){
        matrix.reverse();

        printMatrix();
        return;
    }
    table=getEl('tableData');
    for(var i=0;i<table.tBodies[0].rows.length;i++){
        var row=table.tBodies[0].rows[i]
        matrix[i]=[];
        for(var j=0;j<row.cells.length;j++){
            matrix[i][j]=row.cells[j].innerHTML;
        }
    }
    var function_callback=null;
    switch(sorttype){
        case  'a':function_callback=cmp_alpha;break;
        case  'd':function_callback=cmp_date;break;
    }
    matrix.sort(function_callback);
    printMatrix();
    last=columnid;
}
function printMatrix(){
    for(var i=0;i<table.tBodies[0].rows.length;i++){
        var row=table.tBodies[0].rows[i]
        for(var j=0;j<row.cells.length;j++){
            row.cells[j].innerHTML=matrix[i][j];
        }
    }
}
//Comparator for alphanumberic ignoring case 
//They can be embeded inside the <a href="hello.jsp">  I am a piece of Text </a>
function cmp_alpha(a, b) {
    var strA = (new String(a[columnid])).toLowerCase();
    var strB = (new String(b[columnid])).toLowerCase();
    var re = new RegExp("<a[^>]*>(.*?)</a>");
    var matchA= re.exec(strA);//con
    var matchB= re.exec(strB);//con
    if(!(matchA==null) && matchA.length>0){
        strA=matchA[1];
    }
    if(!(matchB==null) && matchB.length>0){
        strB=matchB[1];
    }

    if (strA < strB) { return -1; }
    else {
        if (strA > strB) { return 1; }
        else { return 0; }
    }
}

//Comparator for Date : They can be embeded inside the <a href="hello.jsp"> 10-06-2006 </a>
function cmp_date(a, b) {
    var datA = new Date();
    var datB = new Date();
    var string_a="";
    var string_b="";
    var re = new RegExp("<a[^>]*>(.*?)</a>");
    var matchA= re.exec(a[columnid]);
    if(!(matchA==null) && matchA.length>0){
        string_a=matchA[1];
    }else{
        string_a=a[columnid];
    }

    var strA=(new String(string_a)).split("-");
    re= new RegExp("<a[^>]*>(.*?)</a>");
    var  matchB= re.exec(b[columnid]);
    if(!(matchB==null) && matchB.length>0){
        string_b=matchB[1];
    }else{
        string_b=b[columnid];
    }

    var strB=(new String(string_b)).split("-");

    /*
         parseInt in javascript will treat number like '09' as a Octal not Base 10
         because of a leading '0'
         so we can force it by adding base argument to a parseInt function

         parseInt('9')=9
         parseInt('09')=0
         parseInt('9',10)=9
         parseInt('09',10)=9

     */

    var month_a=parseInt(strA[0], 10)-1;
    var day_a=parseInt(strA[1], 10);
    var year_a=parseInt(strA[2], 10);


    var month_b=parseInt(strB[0], 10)-1;
    var day_b=parseInt(strB[1], 10);
    var year_b=parseInt(strB[2], 10);

    datA.setFullYear(year_a,month_a,day_a)
    datB.setFullYear(year_b,month_b,day_b)


    var retval=function(){
        if (datA < datB) {
            return -1;
        }  else if (datA > datB) {
            return 1;
        }
        else {
            return 0;
        }
    }();

    //console.info(strA[2]+"-"+strA[0]+"-"+strA[1] +sign+strB[2]+"-"+strB[0]+"-"+strB[1] +"    "+retval +"  -- "+(retval==retvalByYear));

    return	retval;
}

function adjustArrows( ){
    table=getEl('tableData');
    //Figure out the  direction we are going to sort
    if(columnid==last){
        if(dir=='desc'){
            dir='asc'
        }else{
            dir='desc'
        }
    }else{
        dir='desc'
    }
    //console.info('columnIndex =' +columnIndex +" dir="+direction);
    var headerCell=table.tHead.rows[0].cells[columnid];

    for(var i=0;i<table.tHead.rows[0].cells.length;i++){
        table.tHead.rows[0].cells[i].className="sortheader";
    }

    headerCell.className="sortheader clickedCell_"+dir;
    //console.info(headerCell);
}
